/*******************************************************************************
MPLAB Harmony Application Source File
Company:
Microchip Technology Inc.
File Name:
app.c
Summary:
This file contains the source code for the MPLAB Harmony application.
Description:
This file contains the source code for the MPLAB Harmony application. It
implements the logic of the application's state machine and it may call
API routines of other MPLAB Harmony modules in the system, such as drivers,
system services, and middleware. However, it does not call any of the
system interfaces (such as the "Initialize" and "Tasks" functions) of any of
the modules in the system or make any assumptions about when those functions
are called. That is the responsibility of the configuration-specific system
files.
*******************************************************************************/
// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************
#include "app.h"
#include "peripheral/oc/plib_oc.h" //これがないと OC_ID_1がコンパイルエラー
int delay_Clock = 200000000; //200MHz
int tempo_speed = 80; //テンポ=80
int T4 = 750; //四分音符の時間(長さ) T4 = 750 [msec] //T4 = 60 * 1000 / 80;
void delay_us(volatile unsigned int usec) //1μsec遅延
{
volatile int count;
count = (int)(delay_Clock/20000000)*usec;
do //実測 at 200MH (Clock=200000000)
{ //delay_us(1000):1000.4μsec delay_us(100):100.6μsec delay_us(10):10.5μsec delay_us(1):1.5μsec
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP");
asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");
count--;
}while(count != 0);
}
void delay_ms(volatile unsigned int msec) //1msec遅延
{
volatile unsigned int i; //実測:at200MH (Clock=200000000)//delay_ms(1): 1.0006msec delay_ms(100):100.04msec
for(i=0; i<msec; i++)
delay_us(1000);
}
void BuzFreq(volatile int Freq, volatile float Duty) //PWM周波数(Freq) と デューティ(Duty)の設定関数
{
volatile int myPR; //PWM周期のレジスタ値( = PR2 at Timer2)
volatile int myDuty; //PWM出力ON時間に相当するレジスタ値(= OC1RS at OC1)
volatile float PS_T2 = 16; //タイマ2のプリスケール値
PLIB_TMR_Stop(TMR_ID_2); // Disable Timer
//PWM周波数設定
//PR(PRレジスタ値) = Fpbclk(ペリフェラルバスクロック周波数)/Fpwm(PWM周波数)/タイマプリスケール値 - 1;
myPR = (int)(100000000/Freq/PS_T2 -1);
PLIB_TMR_Period16BitSet(TMR_ID_2, myPR); //Set period
myDuty = (int)(PR2 * Duty); //OC1RS = (int)(PR2 * Duty);
PLIB_OC_PulseWidth16BitSet(OC_ID_1, myDuty);
PLIB_TMR_Start(TMR_ID_2); //タイマ2 スタート
}
void ClosePWM(void)
{
PLIB_TMR_Stop(TMR_ID_2);
}
void Do_C4(void) //第四オクターブ ド //C4
{
BuzFreq(261.63,0.5);//ド //261.63Hz
}
void Re_D4(void) //第四オクターブ レ //D4
{
BuzFreq(293.67,0.5); //レ //293.67Hz
}
void Mi_E4(void) //第四オクターブ ミ //E4
{
BuzFreq(329.63,0.5); //ミ //329.63Hz
}
void Fa_F4(void) //第四オクターブ ファ //F4
{
BuzFreq(349.23,0.5); //ファ //349.23Hz
}
void _SoL1(void) //第四オクターブ ソ //G4
{
BuzFreq(392.00,0.5); //ファ //349.23Hz
}
void La_A4(void) //第四オクターブ ラ //A4
{
BuzFreq(440.00,0.7); //ラ //440.00Hz
}
void La_sharpA4(void) //第四オクターブ #ラ(嬰イ) //A#4
{
BuzFreq(466.16,0.7); //ラ //466.16Hz
}
void Si_B4(void) //第四オクターブ シ //B4
{
BuzFreq(493.88,0.4); //シ //493.88Hz
}
void Do_C5(void) //第五オクターブ ド //C5
{
BuzFreq(523.23,0.5); //ド //523.23Hz
}
void Re_D5(void) //第五オクターブ レ
{
BuzFreq(587.34,0.5); //レ //587.34Hz
}
void noSound(void)
{
ClosePWM();
delay_ms(50);
}
// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************
// *****************************************************************************
/* Application Data
Summary:
Holds application data
Description:
This structure holds the application's data.
Remarks:
This structure should be initialized by the APP_Initialize function.
Application strings and buffers are be defined outside this structure.
*/
APP_DATA appData;
// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary callback functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************
/* TODO: Add any necessary local functions.
*/
// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************
/*******************************************************************************
Function:
void APP_Initialize ( void )
Remarks:
See prototype in app.h.
*/
void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;
/* TODO: Initialize your application's state machine and other
* parameters.
*/
DRV_OC0_Enable(); //OC1 イネーブル
}
/******************************************************************************
Function:
void APP_Tasks ( void )
Remarks:
See prototype in app.h.
*/
void APP_Tasks ( void )
{
/* Check the application's current state. */
switch ( appData.state )
{
/* Application's initial state. */
case APP_STATE_INIT:
{
bool appInitialized = true;
if (appInitialized)
{
appData.state = APP_STATE_SERVICE_TASKS;
}
break;
}
case APP_STATE_SERVICE_TASKS:
{
//第1行: うさぎ追いし かの山 -----------------------------------------------------------
//-----第一小節
Fa_F4(); //ファ
delay_ms(T4); //四分音符長さ
noSound();
Fa_F4(); //ファ
delay_ms(T4); //四分音符長さ
noSound();
Fa_F4(); //ファ
delay_ms(T4); //四分音符長さ
noSound();
//------第二小節
_SoL1();//ソ
delay_ms(1125);//付点付四分音符長さ
// delay_ms(T4*1.5);//付点付四分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4/2); //八分音符長さ
noSound();
_SoL1();//ソ
delay_ms(750);
// delay_ms(T4); //四分音符長さ
noSound();
//-------第三小節
La_A4(); //ラ
delay_ms(T4); //四分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4); //四分音符長さ
noSound();
La_sharpA4(); //シ (#ラ)
delay_ms(T4); //四分音符長さ
noSound();
//--------第四小節
Do_C5(); //ド
delay_ms(T4*2); //二分音符長さ
noSound();
delay_ms(T4); //四分休符
//第2行 こぶな釣りし かの川 ---------------------------------------------------------
//--------第一小節
La_sharpA4(); //#ラ
delay_ms(T4);
noSound();
Do_C5(); //ド
delay_ms(T4); //四分音符長さ
noSound();
Re_D5(); //レ
delay_ms(T4); //四分音符長さ
noSound();
//---------第二小節
La_A4(); //ラ
delay_ms(T4*1.5);//付点付四分音符長さ
noSound();
La_sharpA4(); //シ (#ラ)
delay_ms(T4/2); //八分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4);//付点付四分音符長さ
noSound();
//----------第三小節
_SoL1();//ソ
delay_ms(750);
// delay_ms(T4);//付点付四分音符長さ
noSound();
_SoL1();//ソ
// while(1);
delay_ms(750);
// delay_ms(T4);//付点付四分音符長さ
noSound();
Mi_E4(); //第四オクターブ ミ //E4
delay_ms(T4);//付点付四分音符長さ
noSound();
//-----------第四小節
Fa_F4(); //ファ
delay_ms(T4*2); //二分音符長さ
noSound();
delay_ms(T4); //四分休符
//第3行 夢は今も めぐりて
//-----------第一小節
_SoL1();//ソ
delay_ms(T4/2);//八分音符長さ
noSound();
Fa_F4(); //ファ
delay_ms(T4/2); //八分音符長さ
noSound();
_SoL1();//ソ
delay_ms(T4);//四分音符長さ
noSound();
Do_C4(); //ド
delay_ms(T4);//四分音符長さ
noSound();
//------------第二小節
Fa_F4(); //ファ
delay_ms(T4/2); //八分音符長さ
noSound();
_SoL1();//ソ
delay_ms(T4/2);//八分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4);//四分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4);//四分音符長さ
noSound();
//------------第三小節
La_sharpA4(); //シ (#ラ)
delay_ms(T4/2); // 八分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4/2);//八分音符長さ
noSound();
La_sharpA4(); //シ (#ラ)
delay_ms(T4*1.5); //付点付四分音符長さ
noSound();
Re_D5(); //レ
delay_ms(T4/2); //八分音符長さ
noSound();
//-----------第四小節
Do_C5(); //ド
delay_ms(T4/2); //二分音符長さ
noSound();
La_sharpA4(); //シ (#ラ)
delay_ms(T4/2); //八分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4);//四分音符長さ
noSound();
delay_ms(T4); //四分休符
//第4行 忘れがたき 故郷(ふるさと)
//---------第一小節
Do_C5(); //ド
delay_ms(T4); //四分音符長さ
noSound();
Do_C5(); //ド
delay_ms(T4); //四分音符長さ
noSound();
Do_C5(); //ド
delay_ms(T4); //二分音符長さ
noSound();
//----------第二小節
Fa_F4(); //ファ
delay_ms(T4*1.5); //付点付四分音符長さ
noSound();
_SoL1();//ソ
// while(1);
delay_ms(T4/2);//八分音符長さ
noSound();
La_A4(); //ラ
delay_ms(T4);//四分音符長さ
noSound();
//----------第三小節
La_sharpA4(); //シ (#ラ)
delay_ms(T4); //四分音符長さ
noSound();
La_sharpA4(); //シ (#ラ)
delay_ms(T4); //四分音符長さ
noSound();
_SoL1();//ソ
while(1);
delay_ms(T4);//四分音符長さ
noSound();
//-----------第四小節
Fa_F4(); //ファ
delay_ms(T4*2); //二分音符長さ
noSound();
delay_ms(T4); //四分休符
///--------------------------------------------------------------------------------
delay_ms(3000);
break;
}
/* TODO: implement your application state machine.*/
/* The default state should never be executed. */
default:
{
/* TODO: Handle error in application's state machine. */
break;
}
}
}
/*******************************************************************************
End of File
*/